package com.ply.flashsalessystem.service.activityService;

import com.ply.flashsalessystem.entity.pojo.Activity;
import com.ply.flashsalessystem.entity.pojo.Goods;
import com.ply.flashsalessystem.entity.pojo.OrderForGoodsDetaile;
import com.ply.flashsalessystem.entity.pojo.ShopCar;
import com.ply.flashsalessystem.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author XiaoPan
 * date: 2022/5/11 0:47
 * <p>
 * action:  活动 处理类
 */
@Component
public class ActivityHandle {

    @Autowired
    ActivityService activityService;

    @Autowired
    GoodsService goodsService;

    /**
     * 判断 是否有符合活动的吗
     */
    public void pdActivity() {

    }

    /**
     * 看看有没有折扣 有的话
     */
    public List<OrderForGoodsDetaile> activityHandle(List<ShopCar> list) {
        // 判断 那种 折扣

        // 构建 map
        Map<Integer, List<Map<String, Double>>> map = new HashMap<>();
        for (ShopCar shopCar : list) {
            Integer goodsId = shopCar.getGoodsId();
            System.out.println(goodsId);

            Double goodsNum = shopCar.getGoodsNum();
            Goods byId = goodsService.getById(goodsId);
            Double price = byId.getPrice();

            List<Activity> activities = activityService.queryByGoodsId(goodsId);

            for (Activity activity : activities) {
                // 判断活动 是否有效
                if (activityService.pdActivity(activity)) {
                    continue;
                }

                /**
                 * 判断 是否可以参与活动? 达到 满的数量
                 */

                Map<String, Double> map1 = new HashMap<>();
                List<Map<String, Double>> list1 = new ArrayList<>();
                map1.put("goodsId", Double.valueOf(goodsId));
                map1.put("goodsNum", goodsNum);
                map1.put("price", price);
                map1.put("amount", goodsNum * price);

                if (map.get(activity.getActivityId()) == null) {
                    list1.add(map1);
                    map.put(activity.getActivityId(), list1);
                } else {
                    List<Map<String, Double>> maps = map.get(activity.getActivityId());
                    maps.add(map1);
                }
            }
        }


        /**
         * 判断参与 活动是否可以 可以的话就退钱
         */

        Map<Integer, Map<String, Object>> map1 = new HashMap<>();

        for (Map.Entry<Integer, List<Map<String, Double>>> integerListEntry : map.entrySet()) {
            Integer activityId = integerListEntry.getKey();
            Activity activity = activityService.queryById(activityId);
            Double full = activity.getFull();
            Double reduced = activity.getReduced();
            // 满减 没有这个标准
//            Double minStandard = activity.getMinStandard();

            double amount1 = 0;

            List<Map<String, Double>> list1 = integerListEntry.getValue();
            for (Map<String, Double> stringDoubleMap : list1) {
                Double amount = stringDoubleMap.get("amount");
                amount1 += amount;
            }

            if (amount1 < full) {
                //无法 参与此活动
                continue;
            }
//            构建map
            Map<String, Object> map2 = new HashMap<>();

            // 存入商品id
            Set<Double> set = new HashSet<>();
            for (Map<String, Double> stringDoubleMap : list1) {
                set.add(stringDoubleMap.get("goodsId"));
            }
            map2.put("goodsIdSet", set);
            // 可以优惠的价格
            double disAmount = (amount1 / full) * reduced;
            map2.put("disAmount", disAmount);
            map1.put(activityId, map2);
        }

        // 动态规划? 未完成
        Map<Integer, Map<String, Object>> newMap = new HashMap<>();
        double maxAmount = 0;
        double sumAmount = 0;
        // 缺点使用 两个map的话 遍历所有的
        for (Map.Entry<Integer, Map<String, Object>> integerMapEntry : map1.entrySet()) {
            Map<Integer, Map<String, Object>> newMap1 = new HashMap<>();
            Set<Double> set = new HashSet<>();

            Map<String, Object> value = integerMapEntry.getValue();
            double disAmount = (double) value.get("disAmount");
            // 存入
            newMap1.put(integerMapEntry.getKey(), value);

            sumAmount += disAmount;
            Set<Double> goodsIdSet = (Set<Double>) value.get("goodsIdSet");
            // 将 商品id 存进去
            set.addAll(goodsIdSet);
            for (Map.Entry<Integer, Map<String, Object>> int2 : map1.entrySet()) {
                if (Objects.equals(integerMapEntry.getKey(), int2.getKey())) {
                    continue;
                }
                // 这里 判断 重复商品id 不可以参与的
                Set<Double> set1 = (Set<Double>) int2.getValue().get("goodsIdSet");
                boolean a = false;

                for (Double aDouble : set) {
                    for (Double aDouble1 : set1) {
                        if (aDouble.equals(aDouble1)) {
                            a = true;
                            break;
                        }
                    }
                    if (a) {
                        break;
                    }
                }
                if (a) {
                    continue;
                }
                newMap1.put(int2.getKey(), int2.getValue());
                set.containsAll(set1);
                sumAmount += (double) int2.getValue().get("disAmount");
            }
            if (disAmount >= maxAmount) {
                maxAmount = disAmount;
                newMap = newMap1;
            }
            sumAmount = 0;
        }
        for (Map.Entry<Integer, Map<String, Object>> mapEntry : newMap.entrySet()) {
            Integer key = mapEntry.getKey();
            System.out.println("活动id:" + key);
            Map<String, Object> value = mapEntry.getValue();
            for (Map.Entry<String, Object> entry : value.entrySet()) {
                String key1 = entry.getKey();
                Object value1 = entry.getValue();
                System.out.println("key:" + key1 + "value" + value1);
            }
            System.out.println();
        }
        return new ArrayList<OrderForGoodsDetaile>();
    }
}
